package com.gjy.rocketmq;

import org.apache.rocketmq.client.consumer.MQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-09-14 09:00:57
 */
@SpringBootApplication
public class RocketMqApp {

    private static final Logger log = LoggerFactory.getLogger(RocketMqApp.class);

    public static void main(String[] args) throws Exception {

        ConfigurableApplicationContext context = SpringApplication.run(RocketMqApp.class, args);

        ConfigurableEnvironment environment = context.getEnvironment();
        String topic = environment.getProperty("rocket.topic");

        MQPushConsumer consumer = context.getBean(MQPushConsumer.class, "consumer");
        consumer.subscribe(topic, "*");
        consumer.registerMessageListener((MessageListenerConcurrently) (m, c) -> {
            log.info("{}", m);
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        });

        consumer.start();
    }
}
